package com.huarun.module.airconditioning.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.huarun.common.BaseServiceImpl;
import com.huarun.module.airconditioning.bean.AirConditionerApply;
import com.huarun.module.airconditioning.bean.ApplyTime;
import com.huarun.module.airconditioning.bean.UseTime;
import com.huarun.module.airconditioning.dao.AirConditioningDao;
import com.huarun.module.airconditioning.dao.ApplyTimeDao;
import com.huarun.module.airconditioning.service.IAirConditioningService;
import com.huarun.module.applyrecord.service.IApplyRecordService;

/***
 * 
 * @author dzm
 * @创建时间 2018-10-29 16:08
 * @Description 空调加时
 */
@Service
public class AirConditioningServiceImpl extends BaseServiceImpl<AirConditioningDao, AirConditionerApply>
		implements IAirConditioningService {

	@Resource
	private AirConditioningDao airConditioningDao;
	@Resource
	ApplyTimeDao applyTimeDao;
	@Resource
	private IApplyRecordService applyRecordService;

	@Override
	public boolean checkExistence(String company, String roomInfo, List<UseTime> useTimeInfoList,
			HttpServletRequest request) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("company", company);
		params.put("roomInfo", roomInfo);
		List<AirConditionerApply> list = airConditioningDao.findList(params);
		for (AirConditionerApply airConditionerApply : list) {
			//获取运营平台的工单状态
			String status = applyRecordService.getOperateStatus(airConditionerApply.getOperateNo(), request);
			if ("未接受".equals(status) || "已关闭".equals(status)) {
				continue;
			}
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("orderId", airConditionerApply.getOperateNo());
			List<ApplyTime> timeList = applyTimeDao.findList(param);
			for (ApplyTime applyTime : timeList) {
				for (UseTime useTime : useTimeInfoList) {
					boolean startTime = useTime.getStartDate().equals(applyTime.getStartdate())
							&& useTime.getStartTime().equals(applyTime.getStarttime());
					boolean endTime = useTime.getEndDate().equals(applyTime.getEnddate())
							&& useTime.getEndTime().equals(applyTime.getEndtime());
					if (startTime && endTime) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
